#include<stdio.h>
#include<iostream>
#include<string.h>
#define ll long long
#define N 705
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;
const ll INF=1e12;
ll a[N][N]={0};
ll dp[N][N]={0};
ll len_max=0;
int n,m;
int hua(int i,int j)
{
	int len=1;
	if(a[i][j]>=INF)
		return -1;
	if(dp[i][j])
		return dp[i][j];
	if(a[i][j]>a[i][j-1])
		len=MAX(hua(i,j-1)+1,len);
	if(a[i][j]>a[i][j+1])
		len=MAX(hua(i,j+1)+1,len);
	if(a[i][j]>a[i-1][j])
		len=MAX(hua(i-1,j)+1,len);
	if(a[i][j]>a[i+1][j])
		len=MAX(hua(i+1,j)+1,len);
	dp[i][j]=len; 
	return len;
}
ll fun(){
	memset(dp,0,sizeof(dp));
	int i,j,maxlen=0;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			maxlen=MAX(maxlen,hua(i,j));
	return maxlen;
}
int main()
{
	int i,j,k,maxlen=0;
	int A,B,C,D;
	char op;
	cin>>n;
	for(i=0;i<=n+1;i++)
		for(j=0;j<=n+1;j++)
			if((i==0||i==n+1)||(j==0||j==n+1))
				a[i][j]=INF;
			else
				cin>>a[i][j];
	cin>>m;
	while(m--){
		cin>>op;
		if(op=='C'){
			cin>>A>>B>>C;
			a[A][B]=C;
		}
		else if(op=='S'){
			cin>>A>>B>>C>>D;
			for(i=A;i<=C;i++)
				for(j=B;j<=D;j++)
					if(a[i][j]<INF)
						a[i][j]+=INF;
		}
		else if(op=='B'){
			cin>>A>>B>>C>>D;
			for(i=A;i<=C;i++)
				for(j=B;j<=D;j++)
					if(a[i][j]>=INF)
						a[i][j]-=INF;
		}
		else if(op=='Q')
			cout<<fun()<<endl;
	}
}
